package com.springboot.huli.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.springboot.huli.entity.SysUser;
import com.springboot.huli.entity.UserRole;
import com.springboot.huli.entity.vo.UserVo;
import com.springboot.huli.mapper.SysUserMapper;
import com.springboot.huli.mapper.UserRoleMapper;
import com.springboot.huli.qpage.QPage;
import com.springboot.huli.service.ISysUserService;
import com.springboot.huli.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;

@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {

    @Autowired
    private UserRoleMapper userRoleMapper;

    /**
     * 根据账户获取用户信息
     * @param account
     * @return
     */
    public SysUser getByAccount(String account) {
        QueryWrapper<SysUser> queryWrapper=new QueryWrapper<SysUser>();
        queryWrapper.eq("account",account);
        SysUser sysUser=this.getOne(queryWrapper);
        return sysUser;
    }


    /**
     * 保存用户
     * @param user
     * @return
     */
    @Transactional
    @Override
    public Result saveUser(UserVo user) {
        QueryWrapper<SysUser> queryWrapper=new QueryWrapper<SysUser>();
        queryWrapper.eq("account",user.getAccount());
        if(this.getOne(queryWrapper)!=null){
            return Result.reError("注册失败，此账号已经有人注册");
        }
        user.setCtime(new Date());
        user.setUtime(new Date());
        if(this.save(user)){
            //第二步根据选择用户id进行角色用户中间表插入
            if(!StringUtils.isEmpty(user.getRoleids())){
                String[] roleids=user.getRoleids().split(",");
                if(roleids!=null&&roleids.length>=1){
                    for (String roleid : roleids) {
                        UserRole userRole =new UserRole();
                        userRole.setRoleid(Integer.parseInt(roleid));
                        userRole.setUserid(user.getUserid());
                        this.userRoleMapper.insert(userRole);
                    }
                }
            }
            return Result.reOk("恭喜，新增账号成功");
        }
        return Result.reError("新增账号出错");
    }

    /**
     * 获取所有的用户列表
     * @return
     */
    @Override
    public IPage getPageList(QPage qPage, UserVo userVo) {
        IPage iPage = new Page(qPage.getPage(), qPage.getRows());
        QueryWrapper<SysUser> queryWrapper=new QueryWrapper<SysUser>();
        queryWrapper.eq("type",1);
        IPage userList=this.page(iPage,queryWrapper);
        return userList;
    }

    /**
     * 根据用户iD获取用户信息
     * @param userid
     * @return
     */
    @Override
    public UserVo getUserRoleById(Integer userid) {
        return this.userRoleMapper.getUserRoleById(userid);
    }

    /**
     * 修改用户
     * @param user
     * @return
     */
    @Transactional
    @Override
    public Result updateUser(UserVo user) {
        QueryWrapper<SysUser> queryWrapper=new QueryWrapper<SysUser>();
        queryWrapper.eq("account",user.getAccount());
        SysUser newuser=this.getOne(queryWrapper);
        if(newuser!=null&&!newuser.getUserid().equals(user.getUserid())){
            return Result.reError("此账号已经存在，请重新取账号");
        }
        if(this.updateById(user)){
            //第一步先删除中间表
            UserRole userRole=new UserRole();
            QueryWrapper<UserRole> queryWrapper1=new QueryWrapper<UserRole>();
            queryWrapper1.eq("userid",user.getUserid());
            this.userRoleMapper.delete(queryWrapper1);
            //第二步根据选择菜单进行角色菜单中间表插入
            if(!StringUtils.isEmpty(user.getRoleids())){
                String[] roleids=user.getRoleids().split(",");
                if(roleids!=null&&roleids.length>=1){
                    for (String roleid : roleids) {
                        userRole =new UserRole();
                        userRole.setRoleid(Integer.parseInt(roleid));
                        userRole.setUserid(user.getUserid());
                        this.userRoleMapper.insert(userRole);
                    }
                }
            }
            return Result.reOk("恭喜，修改用户成功");
        }
        return Result.reError("修改用户出错");
    }

    /**
     * 启用账号或停用账号
     * @param user
     * @return
     */
    @Transactional
    @Override
    public Result updateUserStatus(UserVo user) {
        SysUser us=this.getById(user.getUserid());
        if(us.getAccount().equals("admin")){
            return Result.reError("超级管理员不能进行删除");
        }
        this.updateById(us);
        return Result.reOk("恭喜，操作成功");
    }

    /**
     * 删除用户
     * @param user
     * @return
     */
    @Transactional
    @Override
    public Result deleteUser(UserVo user) {
        SysUser us=this.getById(user.getUserid());
        if(us.getAccount().equals("admin")){
            return Result.reError("超级管理员不能进行删除");
        }
        this.removeById(us);
        //删除用户对应的角色
        UserRole userRole=new UserRole();
        userRole.setUserid(user.getUserid());
        this.userRoleMapper.deleteById(userRole);
        return Result.reOk("恭喜，删除当前用户成功");
    }


    /**
     * 根据账号获取当前用户是否存在
     * @param type
     * @return
     */
    @Override
    public SysUser getExitAccount(String account,Integer type) {
        QueryWrapper<SysUser> queryWrapper=new QueryWrapper<SysUser>();
        queryWrapper.eq("account",account);
        queryWrapper.eq("type",type);
        return this.getOne(queryWrapper);
    }

}
